package com.beforejava5;

import java.util.concurrent.*;

/**
 * @Description TODO
 * @Author zhouhai
 * @Date2018/9/710:43
 **/
public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //执行器服务,线程池(ThreadPoolExecutor)是它的一种实现
        //线程池是线程复用
        ExecutorService executorService = Executors.newFixedThreadPool(1);

        Future<String> future = executorService.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                return "[Thread: " + Thread.currentThread().getName() + "]Hello,World";
            }
        });

        //等待完成
        while (true) {
            if (future.isDone()) {
                break;
            }
        }

        String s = future.get();

        System.out.println(s);

        executorService.shutdown();
    }


}
